package com.smile.publisher.controller;


import com.smile.publisher.service.ClickHouseService;
import com.smile.publisher.service.ESService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Author:smile
 * Date:2022/2/16
 * Desc:发布数据接口
 */
@RestController
public class PublisherController {
    /*
     访问路径：http://publisher/:8070/realtime-total?date=2020-08-18
     响应数据：[{"id":"dau","name":"新增日活","value":"100},
     {"id":"new_mid","name":"新增设备","value":"200}]
     */
    @Autowired
    ESService esService;

    @Autowired
    ClickHouseService clickHouseService;

    //每日新增日活
    @GetMapping("/realtime-total")
    public Object realTimeTotal(@RequestParam("date") String date) {
        ArrayList<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<>();
        map.put("id", "dau");
        map.put("name", "新增日活");
        Long dauTotals = esService.getDauTotals(date);
        //  System.out.println(dauTotals);
        if (dauTotals != null) {
            map.put("value", dauTotals);
        } else {
            map.put("value", 0L);
        }
        arrayList.add(map);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("id", "new_mid");
        map2.put("name", "新增设备");
        map2.put("value", 100);
        arrayList.add(map2);

        Map<String, Object> orderAmountMap = new HashMap<>();
        map2.put("id", "order_amount");
        map2.put("name", "新增交易额");
        map2.put("value",clickHouseService.selectOrderAmountTotal(date));
        arrayList.add(orderAmountMap);

        return arrayList;
    }

    //每日每时新增日活
    // 访问路径：http://publisher:8070/realhuor-total?date=2022-02-15
    @RequestMapping("/realtime-hour")
    public Object realtimeHour(@RequestParam(value = "id", defaultValue = "-1")
                                       String id, @RequestParam("date") String dt) {
        if (id.equals("dau")) {
            //封装返回的数据
            Map<String, Map> hourMap = new HashMap<>();
            //获取今天日活分时
            Map dauHourTdMap = esService.getDauHourTotals(dt);
            hourMap.put("today", dauHourTdMap);
            //获取昨天日期
            String yd = getYd(dt);
            //获取昨天日活分时
            Map dauHourYdMap = esService.getDauHourTotals(yd);
            hourMap.put("yesterday", dauHourYdMap);
            return hourMap;
        }else if("order_amount".equals(id)){
            //封装返回的数据
            Map<String, Map<String, BigDecimal>> amountMap = new HashMap<>();
            //获取今天日活分时
            List<Map<String, BigDecimal>> maps = clickHouseService.selectOrderAmountHour(dt);
            for(Map map:maps){
                amountMap.put("today",map);
            }
            //获取昨天日期
            String yd = getYd(dt);
            //获取昨天日活分时
            Map dauHourYdMap = esService.getDauHourTotals(yd);
            amountMap.put("yesterday", dauHourYdMap);
            return amountMap;
        }else{
            return null;
        }
    }

    private String getYd(String td) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String yd = null;
        try {
            Date tdDate = dateFormat.parse(td);
            Date ydDate = DateUtils.addDays(tdDate, -1);
            yd = dateFormat.format(ydDate);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("日期格式转变失败");
        }
        return yd;
    }
}
